package com.atguigu.product.service.impl;

import com.atguigu.clients.CategoryClient;
import com.atguigu.param.ProductHotParam;
import com.atguigu.param.ProductIdsParam;
import com.atguigu.pojo.Category;
import com.atguigu.pojo.Product;
import com.atguigu.product.mapper.ProductMapper;
import com.atguigu.product.service.ProductService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.LinkedHashMap;
import java.util.List;

/**
 *
 * 类别的实现类
 */

@Slf4j
@Service

public class ProductServiceImpl implements ProductService {
    /**
     * 单类别名称 查询热门商品 至多7条数据
     * 1. 根据类别名称 调用 feign客户端访问类别服务获取类别的数据
     * 2. 成功 继续根据类别id查询商品数据  [热门 销售量倒序 查询7]
     * 3. 结果封装即可
     *
     * @param categoryName 类别名称
     * @return r
     */


    //引入feign客户端需要,在启动类添加配置注解
    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public R promo(String categoryName) {
        R r = categoryClient.byName(categoryName);

        if (r.getCode().equals(R.FAIL_CODE)) {
            log.info("ProductServiceImpl.promo业务结束，结果:{}","类别查询失败!");
            return r;
        }





        // 类别服务中 data = category --- feign {json}  ----- product服务 LinkedHashMap jackson

//        Category category = (Category) r.getData();
        LinkedHashMap<String,Object> map = (LinkedHashMap<String, Object>) r.getData();

//        Integer categoryId = category.getCategoryId();
        Integer categoryId = (Integer) map.get("category_id");


        //封装查询参数
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.orderByDesc("product_sales");

        IPage<Product> page = new Page<>(1,7);

        // 返回的是包装数据! 内部有对应的商品集合,也有分页的参数 例如: 总条数 总页数等等
        page = productMapper.selectPage(page, queryWrapper);

        List<Product> productList = page.getRecords(); // 指定页的数据
        long total = page.getTotal(); // 获取总条数

        log.info("ProductServiceImpl.promo业务结束，结果:{}",productList);

        return R.ok("数据查询成功", productList);
    }


    /**
     * 多类别热门商品查询 根据类别名称集合! 至多查询7条!
     * 1. 调用类别服务
     * 2. 类别集合id查询商品
     * 3. 结果集封装即可
     *
     * @param productHotParam
     * @return
     */
    @Override
    public R hots(ProductHotParam productHotParam) {

        R r = categoryClient.hots(productHotParam);

        if (r.getCode().equals(R.FAIL_CODE)) {
            log.info("ProductServiceImpl.hots业务结束，结果:{}", r.getMsg());
            return r;
        }


        List<Object> ids = (List<Object>) r.getData();

        // 进行商品数据查询
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("category_id",ids);
        queryWrapper.orderByDesc("product_sales");

        IPage<Product> page = new Page<>(1,7);

        page = productMapper.selectPage(page,queryWrapper);

        List<Product> records = page.getRecords();

        R ok = R.ok("多类别热门商品查询成功!", records);

        log.info("ProductServiceImpl.hots业务结束，结果:{}",ok);

        return ok;
    }


    /**
     * 商品 类别查询集合 列表页
     *
     * @return
     */
    @Override
    public R clist() {
        R r = categoryClient.list();
        log.info("ProductServiceImpl.clist业务结束，结果:{}",r);

        return r;
    }


    /**
     * 通用性业务查询
     * 没有传入类别id 查询全部！
     *
     * @param productIdsParam
     * @return
     */
    @Override
    public R byCategory(ProductIdsParam productIdsParam) {
        // 1.拆分请求参数
        List<Integer> categoryID = productIdsParam.getCategoryID();

        // 2.请求条件封装
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        if (!categoryID.isEmpty()) {
            queryWrapper.in("category_id", categoryID);
        }

        // 3.数据查询
        int currentPage = productIdsParam.getCurrentPage();
        int pageSize = productIdsParam.getPageSize();
        IPage<Product> page = new Page<>(currentPage,pageSize);

        IPage<Product> iPage = productMapper.selectPage(page, queryWrapper);
        // 4.结果封装
        List<Product> productList = iPage.getRecords();
        long total = iPage.getTotal();

        R ok = R.ok(null, productList, total);

        log.info("ProductServiceImpl.byCategory业务结束，结果:{}", ok);
        return ok;
    }
}
